package com.businessportal.web.controllers.blog;

import com.businessportal.web.dataaccess.entities.BlogEntity;
import com.businessportal.web.dataaccess.entities.Login;
import com.businessportal.web.dataaccess.services.BlogEntityService;
import com.businessportal.web.dataaccess.services.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Set;

/**
 * Created by Vadim on 6/25/14.
 */
@WebServlet(urlPatterns = "/securepages/blog_creator.do")
public class BlogCreatorController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        try {

    /*        String dbHost = System.getenv("OPENSHIFT_MYSQL_DB_HOST");
            String dbPort = System.getenv("OPENSHIFT_MYSQL_DB_PORT");
            String appName = System.getenv("OPENSHIFT_APP_NAME");
            String db_url = "jdbc:mysql://" + dbHost + ":" + dbPort + "/" + appName;
            resp.getWriter().println("DbUrl: " + db_url);resp.getWriter().write("OK");
            try{
                Login user = UserService.INSTANCE.CheckLoginPass("login0", "Password");
                if(null == user){
                    user = UserService.INSTANCE.AddNewUser("login0", "Password", "Pupkin");
                }
            }catch(Exception e){
                resp.getWriter().println(e.getMessage());
                return;
            }*/


            req.setCharacterEncoding("UTF-8");

            HttpSession session = req.getSession();
            Login user = (Login)session.getAttribute("user");

            String showBogId = req.getParameter("showBogId");

            if(null != showBogId && !showBogId.isEmpty()){

                BlogEntity blogToShow = BlogEntityService.INSTANCE.getBlogById(Long.parseLong(showBogId));

                req.setAttribute("blogid", blogToShow.getId());
                req.setAttribute("blogtitle", blogToShow.getBlogTitle());
                req.setAttribute("blogtext", blogToShow.getBlogText());
                req.setAttribute("isPrivate", blogToShow.isPrivate());
            } else {
                req.setAttribute("blogid", "");
                req.setAttribute("blogtitle", "");
                req.setAttribute("blogtext", "");
            }

            req.setAttribute("blogs", user.getBlogs());

        } catch (Exception e){
            req.setAttribute("errors", e.getMessage());
        }

        getServletContext().getRequestDispatcher("/securepages/blog_creator.jsp").forward(req, resp);
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        try {

            req.setCharacterEncoding("UTF-8");

            String blogid = req.getParameter("blogid");
            String blogTitle = req.getParameter("blogtitle");
            String blogText = req.getParameter("blogtext");
            boolean isPrivate = Boolean.parseBoolean(req.getParameter("isPrivate"));

            HttpSession session = req.getSession();
            Login user = (Login)session.getAttribute("user");

            if(null != blogid && !blogid.isEmpty()){

                BlogEntity blogToShow = BlogEntityService.INSTANCE.getBlogById(Long.parseLong(blogid));

                blogToShow.setBlogTitle(blogTitle);
                blogToShow.setBlogText(blogText);
                blogToShow.setPrivate(isPrivate);

                BlogEntityService.INSTANCE.updateBlog(blogToShow);

                req.setAttribute("blogid", blogToShow.getId());
                req.setAttribute("blogtitle", blogToShow.getBlogTitle());
                req.setAttribute("blogtext", blogToShow.getBlogText());
                req.setAttribute("isPrivate", blogToShow.isPrivate());
            } else {
                BlogEntity bl = BlogEntityService.INSTANCE.AddNewBlog(blogTitle, blogText, user, isPrivate);

                req.setAttribute("blogid", bl.getId());
                req.setAttribute("blogtitle", bl.getBlogTitle());
                req.setAttribute("blogtext", bl.getBlogText());
                req.setAttribute("isPrivate", bl.isPrivate());
            }

            req.setAttribute("blogs", BlogEntityService.INSTANCE.getBlogsForUser(user));

        } catch(Exception e){
            req.setAttribute("errors", e.getMessage());
        }

        getServletContext().getRequestDispatcher("/securepages/blog_creator.jsp").forward(req, resp);
    }
}
